package client.handler;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import client.child.thread.ClientChildThread;
import qq.login.gui.BackMessage;
import qq.login.gui.LoginGui;
import qq.message.DataPackage;
import qq.message.DataPackage.MessageType;
import qq.panel.gui.simple.PanelSimpleGUI;

public class ClientHandler {
	
	private Socket socket;
	private OutputStream os;
	private InputStream is;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;
	private PanelSimpleGUI psg;
	public ObjectInputStream getOis() {
		return ois;
	}

	public void setOis(ObjectInputStream ois) {
		this.ois = ois;
	}

	public ObjectOutputStream getOos() {
		return oos;
	}

	public void setOos(ObjectOutputStream oos) {
		this.oos = oos;
	}

	public void client() throws UnknownHostException, IOException, InterruptedException, ClassNotFoundException{
		//建立连接
		socket=new Socket("127.0.0.1",8888);
		os=socket.getOutputStream();
		is=socket.getInputStream();
		oos=new ObjectOutputStream(os);
		ois = new ObjectInputStream(is);
		
		LoginGui lg=new LoginGui(); //启动界面申明
		lg.createLoginGUI();   //启动界面
		lg.addAction(this);   //启动Action
		BackMessage buttonStaue=new BackMessage();
		while(true)
		{
			Thread.sleep(1000);
			buttonStaue=lg.getBackMessage();
			if(buttonStaue.getButtonStaue()!=0)
			{
				break;
			}
		}
		handlerButtonStaue(buttonStaue,lg);//处理不同的状态
	}
	
	public void handlerButtonStaue(BackMessage buttonStaue,LoginGui lg ) throws IOException, ClassNotFoundException, InterruptedException{
		
		if(buttonStaue.getButtonStaue()==-1)
		{
			System.exit(0);
		}
		else
		{
			/*
			 * 发送login请求
			 * 设置相关的信息
			 */
			DataPackage data=new DataPackage();
			data.setFromName(buttonStaue.getUserName());
			data.setToName("server");
			data.setMessageType(MessageType.LOGIN);
			/*
			 * 预留密码验证功能
			 */
			data.setMessageData(buttonStaue.getPassWord());
			data.setTime(System.currentTimeMillis());
			System.out.println(data.toString());
			//发送login请求
			oos.writeObject(data);
			System.out.println("send login data succeed...");
			data=(DataPackage) ois.readObject();
			System.out.println("get replay");
			if(data.getMessageType()==MessageType.LOGIN&&data.getFromName().equals("server")&&data.getMessageData().equals("ack"))
			{
				
				/*
				 * 开启子线程
				 * 开启下级界面
				 * 关闭当前界面 fg.close();
				 */
				System.out.println("game over");
				lg.close();
				System.out.println("start main gui");
				psg=new PanelSimpleGUI(buttonStaue.getUserName());   //主界面申明，有需要时启动
				ClientChildThread cct=new ClientChildThread(buttonStaue.getUserName(),oos,psg);
				new Thread(cct).start();
				while(true)
				{
//					Thread.sleep(100);
					handleRecevieData(psg);
				}
			}
			else
			{
				lg.getLoginInfo().setText("Account or Password error,Try again...");
			}
			
		}
	}
	
	public void handleRecevieData(PanelSimpleGUI psg) throws ClassNotFoundException, IOException{
		ReceiveHandler rh=new ReceiveHandler();
		DataPackage receive=null;
		while(true)
		{
			receive = (DataPackage) ois.readObject();
			System.out.println("get one message");
			switch(receive.getMessageType())
			{
			case MESSAGE:  rh.handleReceiveMessage(receive, psg);
				break;
			case SHAKE:    rh.handleReceiveShake(receive, psg);
				break;
			case LIST:     rh.handleReceiveList(receive, psg);
				break;
			case LOGOUT:   rh.handleReceiveLogout(receive, psg);
				break;
			default:       System.out.print("");
				break;
			}
		}
	}

	
}
